package com.jinyou.utils.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 类名称：AjaxResult
 * 类描述：   工具类   统一返回Json字符串的格式
 * 创建人：Jahnke 1029777564@qq.com
 * 创建时间：2015年8月14日 下午6:36:24
 * 修改备注：
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AjaxResult<T> implements Serializable {
    private static final long serialVersionUID = -6890776123674482761L;

    @ApiModelProperty("状态值：1成功，0失败")
    private Integer status;
    @ApiModelProperty("返回数据")
    private T info;
    @ApiModelProperty("错误类型")
    private String type;
    @ApiModelProperty("错误信息")
    private String error;

    //返回成功提示信息
    public static String successResult(String info) {
        StringBuffer buffer = new StringBuffer();
        if (info == null || info.equals("")) {
            buffer.append("{\"status\":1}");
        } else {
            buffer.append("{\"status\":1,\"info\":\"").append(info).append("\"}");
        }
        return buffer.toString();
    }

    //只返回成功
    public static String successResult() {
        return "{\"status\":1}";
    }

    //返回参数 以及对应的属性
    public static String successResult(String str, Object value) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{\"status\":1");
        if (str != null && !str.equals("")) {
            buffer.append(",\"").append(str).append("\":\"")
                    .append(value == null ? "" : value.toString())
                    .append("\"");
        }
        buffer.append("}");
        return buffer.toString();
    }

    //成功。返回单个对象
    public static String successResult(Object obj) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{\"status\":1,")
                .append("\"info\":")
                .append(JSONObject.fromObject(obj))
                .append("}");
        return buffer.toString();
    }

    public static String successResult(List<?> list) {
        List actualList = list == null ? Collections.EMPTY_LIST : list;
        JSONObject result = new JSONObject();
        result.put("status", 1);
        result.put("size", actualList.size());
        result.put("data", JSONArray.fromObject(actualList));
        return result.toString();
    }

    //成功。返回单个对象
    public static String successResult2(Object obj) {
        return JSONObject.fromObject(obj).toString();
    }

    //成功。返回列表
    public static String successResult(int size, List<?> list) {
        StringBuffer buffer = new StringBuffer();
        if (size == 0) {
            buffer.append("{\"status\":1,\"size\":")
                    .append(0)
                    .append(",\"data\":[]}");
        } else {
            buffer.append("{\"status\":1,\"size\":")
                    .append(size)
                    .append(",\"data\":")
                    .append(JSONArray.fromObject(list))
                    .append("}");
        }
        return buffer.toString();
    }

    //成功。返回列表
    public static String successResult(List<?> list, long count) {
        list = list == null ? Collections.emptyList() : list;
        StringBuffer buffer = new StringBuffer();
        buffer.append("{\"status\":1,\"size\":").append(list.size())
                .append(",\"totalCount\":").append(count)
                .append(",\"data\":")
                .append(JSONArray.fromObject(list))
                .append("}");
        return buffer.toString();
    }

    public static String successResult(int size, List<?> list, String key, int value) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{\"status\":1,\"size\":")
                .append(size)
                .append(",\"").append(key).append("\":").append(value).append(",\"data\":")
                .append(list == null ? "[]" : JSONArray.fromObject(list))
                .append("}");
        return buffer.toString();
    }

    //成功。 返回列表和总数据
    @Deprecated
    public static String successResult(long count, int size, List<?> list) {
        StringBuffer buffer = new StringBuffer();
        if (count == 0) {
            buffer.append("{\"status\":1,\"totalCount\":0,\"size\":0,\"data\":[]}");
        } else if (size == 0) {
            buffer.append("{\"status\":1,\"totalCount\":").append(count).append(",\"size\":0,\"data\":[]}");
        } else {
            buffer.append("{\"status\":1,\"totalCount\":").append(count).append(",\"size\":")
                    .append(size)
                    .append(",\"data\":")
                    .append(JSONArray.fromObject(list))
                    .append("}");
        }
        return buffer.toString();
    }

    //错误，返回错误提示
    public static String errorResult(String info) {
        StringBuffer buffer = new StringBuffer();
        if (info == null || info.equals("")) {
            buffer.append("{\"status\":0}");
        } else {
            buffer.append("{\"status\":0,\"type\":\"comError\",\"error\":\"").append(info).append("\"}");
        }
        return buffer.toString();
    }

    //返回错误类型和错误信息
    public static String errorResult(String info, String errorType) {
        StringBuffer buffer = new StringBuffer();
        if (info == null || info.equals("")) {
            buffer.append("{\"status\":0}");
        } else {
            buffer.append("{\"status\":0,\"type\":\"").append(errorType).append("\",\"error\":\"").append(info).append("\"}");
        }
        return buffer.toString();
    }


    //错误，返回当前帐号一退出type
    public static String errorLogoutResult() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{\"status\":0,\"type\":\"notLogin\",\"error\":\"当前帐号失效，请重新登录\"}");
        return buffer.toString();
    }

    public final static String ErrorType_NeedCalling = "nendCalling";        //错误类型
    public final static String ErrorType_OrderExist = "unfinshedOrderExist";            //错误类型，存在未完成的订单
    public final static String ErrorType_LongTishi = "longtishi";   //长时间提示
    public final static String ErrorType_First_Login = "firstLogin";   // 首次登陆

}
